﻿using Dapper;
using Student.Model;
using Student.Common;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Student.DAL
{
	/// <summary>
	/// UserToken的数据操作类
	/// </summary>
	public partial class UserTokenDAO
	{
        /// <summary>
		/// 添加单条记录
		/// </summary>
		/// <param name="userToken">实体</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Insert(UserToken userToken, IDbTransaction? trans = null)
        {
            var sql = "INSERT INTO UserToken(UserDataID, UserClient, Token, CreateTime, ExpiredTime) VALUES (@UserDataID, @UserClient, @Token, @CreateTime, @ExpiredTime);SELECT CAST(SCOPE_IDENTITY() AS int)";

            if (trans != null)
            {
                var conn = trans.Connection;
                var userTokenID = conn.Query<int>(sql, userToken, trans).Single();
                userToken.UserTokenID = userTokenID;
                var result = 1;
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                var userTokenID = conn.Query<int>(sql, userToken).Single();
                userToken.UserTokenID = userTokenID;
                var result = 1;
                conn.Close();
                return result;
            }
        }
        
        /// <summary>
		/// 添加多条记录
		/// </summary>
		/// <param name="list">列表</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Insert(List<UserToken> list, IDbTransaction? trans = null)
        {
            var sql = "INSERT INTO UserToken(UserDataID, UserClient, Token, CreateTime, ExpiredTime) VALUES (@UserDataID, @UserClient, @Token, @CreateTime, @ExpiredTime)";
            int result;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, list, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, list);
                conn.Close();
                return result;
            }
        }
        
        /// <summary>
		/// 编辑单条记录
		/// </summary>
		/// <param name="userToken">实体</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Update(UserToken userToken, IDbTransaction? trans = null)
        {
            var sql = "UPDATE UserToken SET UserDataID = @UserDataID, UserClient = @UserClient, Token = @Token, CreateTime = @CreateTime, ExpiredTime = @ExpiredTime WHERE UserTokenID = @UserTokenID";
            int result;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, userToken, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, userToken);
                conn.Close();
                return result;
            }
        }

        /// <summary>
		/// 编辑多条记录
		/// </summary>
		/// <param name="list">实体列表</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Update(List<UserToken> list, IDbTransaction? trans = null)
        {
            var sql = "UPDATE UserToken SET UserDataID = @UserDataID, UserClient = @UserClient, Token = @Token, CreateTime = @CreateTime, ExpiredTime = @ExpiredTime WHERE UserTokenID = @UserTokenID";
            int result;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, list, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, list);
                conn.Close();
                return result;
            }
        }
        
        /// <summary>
		/// 删除单条记录
		/// </summary>
		/// <param name="userTokenID">主键</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Delete(int userTokenID, IDbTransaction? trans = null)
        {
            var sql = "DELETE UserToken WHERE UserTokenID = @UserTokenID";
            int result;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, new { UserTokenID = userTokenID }, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, new { UserTokenID = userTokenID });
                conn.Close();
                return result;
            }
        }

        /// <summary>
		/// 删除单条记录
		/// </summary>
		/// <param name="userToken">实体</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Delete(UserToken userToken, IDbTransaction? trans = null)
        {
            var sql = "DELETE UserToken WHERE UserTokenID = @UserTokenID";
            int result;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, userToken, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, userToken);
                conn.Close();
                return result;
            }
        }
        
        /// <summary>
		/// 获取单条记录
		/// </summary>
		/// <param name="userTokenID">主键</param>
		/// <returns>实体</returns>
        public UserToken GetInfo(int userTokenID)
        {
            var sql = "SELECT * FROM UserToken WHERE UserTokenID = @UserTokenID";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                var model = conn.QueryFirstOrDefault<UserToken>(sql, new { UserTokenID = userTokenID });
                return model;
            }
        }

        /// <summary>
		/// 获取列表
		/// </summary>
		/// <returns>实体列表</returns>
        public List<UserToken> GetList()
        {
            var sql = "SELECT * FROM UserToken";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                var q = conn.Query<UserToken>(sql);
                return q.ToList();
            }
        }
        
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="orderBy">排序规则</param>
        /// <param name="whereClause">条件语句</param>
        /// <returns>分页列表</returns>
        public PagedList<UserToken> GetList(int pageIndex, int pageSize, string? whereClause = null, string? orderBy = null)
        {
            if (!string.IsNullOrEmpty(whereClause))
            {
                whereClause = " WHERE " + whereClause;
            }
            if (string.IsNullOrEmpty(orderBy))
            {
                orderBy = "UserTokenID DESC";
            }
            var startItem = pageIndex * pageSize + 1;
            var endItem = (pageIndex + 1) * pageSize;

            var sql = $@"SELECT COUNT(UserTokenID) FROM UserToken {whereClause} 
SELECT *
FROM(SELECT ROW_NUMBER() OVER(ORDER BY {orderBy}) AS RowNum, *
          FROM UserToken
          {whereClause}
        ) AS result
WHERE RowNum >= {startItem}
    AND RowNum <= {endItem}
ORDER BY RowNum";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                int count = 0;
                var list = new List<UserToken>();
                using (var q = conn.QueryMultiple(sql))
                {
                    count = q.Read<int>().Single();
                    list = q.Read<UserToken>().ToList();
                }
                conn.Close();

                var pagedList = new PagedList<UserToken>(list, pageIndex, pageSize, count);
                return pagedList;
            }
        }

		/// <summary>
		/// UserToken的数据操作类
		/// </summary>
		public UserTokenDAO()
		{
		}
	}
}

